package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dao.AdminDao;
import cn.edu.cqupt.cxcy.dto.AdminImpl;
import cn.edu.cqupt.cxcy.util.JsonUtil;
import cn.edu.cqupt.cxcy.util.Validate;


/**
 * 主页-【场地预约】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class AdminAjaxFenye extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String html = req.getParameter("html"); //来自哪个页面
		double count = 1; //总记录数
		int page = Validate.stringIsNO(req.getParameter("page"))?Integer.parseInt(req.getParameter("page")):1; //当前页
		if(page < 1) {
			page  = 1;
		}
		int type = Validate.stringIsNO(req.getParameter("type"))?Integer.parseInt(req.getParameter("type")):255; //类别
		AdminDao admin = new AdminImpl();
		JsonUtil json = new JsonUtil(); 
		String str = null;
		List<Object> lists = null;
		//场地预约  admin/fenye?html=cdyy&&type=?&&status=?&&page=?
		//新闻公告  admin/fenye?html=news&&type=?&&page=?
		//导师          admin/fenye?html=teac&&type=?&&page=?
		//团队          admin/fenye?html=team&&type=?&&page=?
		//投资          admin/fenye?html=inve&&type=?&&page=?
		//项目          admin/fenye?html=pros&&status=?&&page=?
		//日历          admin/fenye?html=cale&&day=?
		
		if("cdyy".equals(html)) {
			int status = Validate.stringIsNO( req.getParameter("status"))?Integer.parseInt( req.getParameter("status")):255; //场地类别
			lists = admin.cDList( AdminDao.CDYY_PAGING, page, type, status);
			count  = Math.ceil( (double)admin.countCd( str, status) / AdminDao.CDYY_PAGING);
		} else if("news".equals(html)) {
			lists = admin.noticeListEscapeHtml( page, AdminDao.NEWS_PAGING, type);
			count = Math.ceil( (double)admin.countNotice(type) / AdminDao.NEWS_PAGING);;
		} else if("teac".equals(html)) {
			lists= admin.tutorList( type, page, AdminDao.TEAC_PAGING);
			count = Math.ceil( (double)admin.countTutor(type) / AdminDao.TEAC_PAGING);
		} else if("team".equals(html)) {
			lists = admin.teamList( page, AdminDao.TEAM_PAGING, type);
			count = Math.ceil( (double)admin.countTeam( type) / AdminDao.TEAM_PAGING);
		} else if("inve".equals(html)) {
			lists = admin.investorList( page, AdminDao.INVE_PAGING);
			count = Math.ceil( (double)admin.countInve() / AdminDao.INVE_PAGING);
		} else if("pros".equals(html)) {
			int status = Validate.stringIsNO(req.getParameter("status"))?Integer.parseInt(req.getParameter("status")):255; //类别
			lists = admin.proList( status, page, AdminDao.PROS_PAGING);
			count  = Math.ceil( (double)admin.countPro(status) / AdminDao.PROS_PAGING);
		} else if("cale".equals(html)) {
			int day = Validate.stringIsNO( req.getParameter("day"))?Integer.parseInt( req.getParameter("day")):0; //场地类别
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(new Date());
	        rightNow.add( Calendar.DAY_OF_YEAR, day);//日期加10天
	        Date dt1=rightNow.getTime();
	        lists = admin.caleList(dt1, type);
		}
		resp.setContentType("application/json;charset=utf-8");
		resp.setHeader("pragma","no-cache");
		resp.setHeader("cache-control","no-cache");
		json.put( "paging", lists); 
		json.put( "count", (int)count);
		PrintWriter out = resp.getWriter();
		out.print(json);
		out.flush();
		out.close();
	}
}
